package com.anti.modular.bus.houseleaselive.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.core.utils.StringUtils;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.houseleaselive.entity.HouseLeaseLive;
import com.anti.modular.bus.houseleaselive.enums.HouseLeaseLiveExceptionEnum;
import com.anti.modular.bus.houseleaselive.param.HouseLeaseLiveParam;
import com.anti.modular.bus.houseleaselive.result.HouseLeaseLiveResult;
import com.anti.modular.bus.houseleaselive.service.HouseLeaseLiveService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 房间租约同住人service接口实现类
 *
 * @author anti
 * @date 2021-05-06 12:22:22
 */
@Service
public class HouseLeaseLiveServiceImpl implements HouseLeaseLiveService {
    @Resource
    DBUtils dbUtils;

    @Override
    public HouseLeaseLive getById(HouseLeaseLiveParam houseLeaseLiveParam){
        String sql="select * from house_lease_live where id =? ";
        return dbUtils.get(sql,HouseLeaseLive.class,houseLeaseLiveParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,HouseLeaseLiveParam houseLeaseLiveParam) {
        StringBuilder sb=new StringBuilder("select * from house_lease_live where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(houseLeaseLiveParam)) {
            // 根据同住人姓名 查询
            if (ObjectUtil.isNotEmpty(houseLeaseLiveParam.getName())) {
               sb.append(" and name = ?");
               param.add(houseLeaseLiveParam.getName());
            }
            // 根据同住人联系方式 查询
            if (ObjectUtil.isNotEmpty(houseLeaseLiveParam.getPhone())) {
               sb.append(" and phone = ?");
               param.add(houseLeaseLiveParam.getPhone());
            }
            // 根据身份证号 查询
            if (ObjectUtil.isNotEmpty(houseLeaseLiveParam.getIdCard())) {
               sb.append(" and id_card = ?");
               param.add(houseLeaseLiveParam.getIdCard());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<HouseLeaseLive> list(HouseLeaseLiveParam houseLeaseLiveParam) {
        String sql="";
        return dbUtils.query(sql,HouseLeaseLive.class);
    }

    @Override
    public List<HouseLeaseLive> listByLeaseId(Long leaseId) {
        String sql="select * from house_lease_live where lease_id =? and status =0 ";
        return dbUtils.query(sql,HouseLeaseLive.class,leaseId);
    }

    @Override
    public void add(HouseLeaseLiveParam houseLeaseLiveParam) {
        HouseLeaseLive houseLeaseLive = new HouseLeaseLive();
        BeanUtil.copyProperties(houseLeaseLiveParam, houseLeaseLive);
        dbUtils.insert(houseLeaseLive);
    }

    @Override
    public void add(HouseLeaseLive houseLeaseLive) {
        dbUtils.insert(houseLeaseLive);
    }

    public void delete(HouseLeaseLiveParam houseLeaseLiveParam) {
        String sql="delete  from house_lease_live where id =? ";
        dbUtils.execSql(sql,houseLeaseLiveParam.getId());
    }

    @Override
    public void deleteByLeaseId(Long id) {
        String sql="delete  from house_lease_live where lease_id =? ";
        dbUtils.execSql(sql,id);
    }

    public void edit(HouseLeaseLiveParam houseLeaseLiveParam) {
        HouseLeaseLive houseLeaseLive = this.queryHouseLeaseLive(houseLeaseLiveParam);
        BeanUtil.copyProperties(houseLeaseLiveParam, houseLeaseLive);
        dbUtils.update(houseLeaseLive);
    }

    @Override
    public HouseLeaseLive detail(HouseLeaseLiveParam houseLeaseLiveParam) {
        return this.queryHouseLeaseLive(houseLeaseLiveParam);
    }

    @Override
    public List<HouseLeaseLiveResult> getTzrListByLeaseId(Long leaseId) {
        String sql="select * from house_lease_live where lease_id =? and status=0";
        List<HouseLeaseLiveResult> query = dbUtils.query(sql, HouseLeaseLiveResult.class, leaseId);
        for (int i = 0; i < query.size(); i++) {
            HouseLeaseLiveResult houseLeaseLiveResult = query.get(i);
            houseLeaseLiveResult.setKey(houseLeaseLiveResult.getId()+"");
            String idCardImagesFront = houseLeaseLiveResult.getIdCardImagesFront();
            if (StringUtils.isNotBlank(idCardImagesFront)){
                houseLeaseLiveResult.setSrc1("/api/sysFileInfo/preview?id="+idCardImagesFront);
            }
            String idCardImagesBack = houseLeaseLiveResult.getIdCardImagesBack();
            if (StringUtils.isNotBlank(idCardImagesBack)){
                houseLeaseLiveResult.setSrc2("/api/sysFileInfo/preview?id="+idCardImagesBack);
            }
        }
        return query;
    }

    /**
     * 获取房间租约同住人
     *
     * @author anti
     * @date 2021-05-06 12:22:22
     */
    private HouseLeaseLive queryHouseLeaseLive(HouseLeaseLiveParam houseLeaseLiveParam) {
        HouseLeaseLive houseLeaseLive = this.getById(houseLeaseLiveParam);
        if (ObjectUtil.isNull(houseLeaseLive)) {
            throw new BusinessException(HouseLeaseLiveExceptionEnum.NOT_EXIST);
        }
        return houseLeaseLive;
    }


}
